#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/21 11:33
# @Author  : 王凯
# @File    : project_clean.py
# @Project : spider-man
import datetime
import re
import warnings

import pandas as pd

from apps.tender_project.tender_project.clean import BaseCleanModel

warnings.filterwarnings("ignore")


def project_name(data):
    if data is None:
        return None
    data = re.sub(r"项目名称[：:为]", "", data)
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）-]'

    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i
        data = re.sub(i, '', data)
    return data


def register_num(data):
    if data is None:
        return None
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）-]'
    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i
        data = re.sub(i, '', data)
    zhongwen = '[\u4e00-\u9fa5]'
    zhongwen_list = re.findall(zhongwen, data)
    for i in zhongwen_list:
        data = re.sub(i, '', data)
    return data


def budget_amount(data):
    if data is None:
        return None
    data = data * 10000

    # arabic_num = transform(data)

    return data


class ProjectClean(BaseCleanModel):

    def process_field_test(self, df_pro: pd.DataFrame) -> pd.DataFrame:
        """
        具体的清洗逻辑
        """
        for index, row in df_pro.iterrows():
            df_pro["project_name"][index] = project_name(row["project_name"])
            df_pro["register_num"][index] = register_num(row["register_num"])
            df_pro["budget_amount"][index] = budget_amount(row["budget_amount"])

        return df_pro

    def run(self, id_list):
        df_src = self.get_data(id_list)  # 原始数据
        df_src = self.process_field_test(df_src)  # 一层数据流转处理
        # todo 详细的清洗规则
        if not df_src.empty:
            # 保存
            df_src = df_src.drop(columns=["id"])
            datas = df_src.to_dict("records")
            update_columns = []  # 要更新的字段 按唯一索引更新
            self.mysql_db_origin.add_batch_smart(self.target_table, datas, update_columns=update_columns)


if __name__ == "__main__":
    test = ProjectClean(
        origin_table="net_project_info", target_table="net_project_info_new",
        redis_task_keys="net_project_info:wash:task"
    )
    test.time_wash(
        (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    )
